package com.zq.widget.inverted.game.role.equipment.weapon.gun.bullet;

import android.animation.FloatEvaluator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.view.animation.LinearInterpolator;

import com.zq.widget.inverted.game.role.Role;
import com.zq.widget.inverted.game.role.anim.Anim;
import com.zq.widget.inverted.game.role.anim.AnimProxy;
import com.zq.widget.inverted.game.role.anim.OnAnimUpdateListener;
import com.zq.widget.inverted.game.role.anim.RepeatMode;
import com.zq.widget.inverted.game.role.damage.Damage;
import com.zq.widget.inverted.game.role.damage.DamageProxy;
import com.zq.widget.inverted.game.role.hp.HP;
import com.zq.widget.inverted.game.role.hp.HPProxy;

/**
 * Created by zhangqiang on 2018/3/30.
 */
public class LaserBullet extends Role implements Damage,HP{

    private Damage damageProxy;
    private HP hpProxy;
    private Paint paint = new Paint();
    private int color;
    private int width;
    private int height;
    private RectF laserRect = new RectF();
    private float animateValue;

    public LaserBullet(Context context, int color, int width,int height) {
        super(context);
        this.color = color;
        this.width = width;
        this.height = height;
        damageProxy = new DamageProxy(20);
        hpProxy = new HPProxy(1000 * 1000);
        setBounds(0,0,width,height);
    }

    @Override
    protected void onDraw(Canvas canvas) {

        RectF bounds = bounds();
        laserRect.left = bounds.centerX() - width/2;
        laserRect.top = bounds.bottom - 10 - height;
        laserRect.right = laserRect.left + width;
        laserRect.bottom = laserRect.top + height;

        paint.reset();
        paint.setColor(color);
        paint.setAntiAlias(true);
        paint.setShadowLayer(10,10 + 10 * animateValue,0, Color.argb((int)(255 * animateValue),Color.red(color),Color.green(color),Color.blue(color)));
        canvas.drawRect(laserRect,paint);
    }

    @Override
    protected Anim<?> onCreateAnim() {
        return AnimProxy.ofFloat(0,1)
                .setDuration(1000)
                .setTypeEvaluator(new FloatEvaluator())
                .setOnAnimUpdateListener(new OnAnimUpdateListener<Number>() {


                    @Override
                    public void onAnimUpdate(Anim<Number> anim) {
                        animateValue = anim.getAnimateValue().floatValue();
                    }
                })
                .setInterpolator(new LinearInterpolator())
                .setRepeatMode(RepeatMode.REVERSE)
                .setRepeatCount(-1);
    }

    @Override
    public boolean isFinish() {
        return super.isFinish() || getHp() <= 0;
    }

    @Override
    public long damage() {
        return damageProxy.damage();
    }

    @Override
    public long totalHp() {
        return hpProxy.totalHp();
    }

    @Override
    public long getHp() {
        return hpProxy.getHp();
    }

    @Override
    public boolean addHp(long hp) {
        return hpProxy.addHp(hp);
    }

    @Override
    public boolean reduceHp(long hp) {
        return hpProxy.reduceHp(hp);
    }
}
